import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const Login = ()=>import("components/Login");
const Home = ()=>import("components/Home");
const Welcome = ()=>import("components/Welcome");

const User = ()=>import("components/homeComps/user/User");

const Roles = ()=>import("components/homeComps/power/Roles");
const Powerlist = ()=>import("components/homeComps/power/Powerlist");

const Goodslist = ()=>import("components/homeComps/goods/Goodslist");
const Goodsparams = ()=>import("components/homeComps/goods/Goodsparams");
const Goodssort = ()=>import("components/homeComps/goods/Goodssort");
const Addgoods = ()=>import("components/homeComps/goods/Addgoods")

const Orderslist = ()=>import("components/homeComps/orders/Orderslist");

const Datachart = ()=>import("components/homeComps/data/Datachart");

const routes = [
  {
    path: "/",
    redirect: "/login"
  },
  {
    path: "/login",
    component: Login
  },
  {
    path: "/home",
    component: Home,
    redirect: "/welcome",
    children: [
      {
        path: "/welcome",
        component: Welcome,
      },
      {
        path: "/users",
        component: User,
      },
      {
        path: "/roles",
        component: Roles
      },
      {
        path: "/rights",
        component: Powerlist
      },
      {
        path: "/roles",
        component: Roles
      },
      {
        path: "/goods",
        component: Goodslist
      },
      {
        path: "/params",
        component: Goodsparams
      },
      {
        path: "/categories",
        component: Goodssort
      },
      {
        path: "/orders",
        component: Orderslist
      },
      {
        path: "/reports",
        component: Datachart
      },
      {
        path: "/goods/add",
        component: Addgoods
      }

    ]
  }
]

const router = new VueRouter({
  routes,
  mode: "history"
})

router.beforeEach((to, from, next)=>{
  if(to.path === "/login") return next();
  const tokenStr = window.sessionStorage.getItem("token");
  if(!tokenStr) return next("/login");
  next();
})

export default router
